<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;

class UserNode extends Model
{
    protected $table = 'user_node';
    public $timestamps = false;

    public function getCreateTimeAttribute($value)
    {
        return date('Y-m-d H:i:s', $value);
    }
    public function getMintTimeAttribute($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    // 每个节点的算力
    public function getNodePower()
    {
        return 100;
    }

    // 最多购买节点数
    public function getMaxBuyNodeNum()
    {
        return 20;
    }

    // 后台添加的算力
    public function getAdminNodePower()
    {
        // return 100;
        return Dictionary::get_quota_power();
    }

    public const NODE_SN_ID = [
        '1' => '一',
        '2' => '二',
        '3' => '三',
        '4' => '四',
        '5' => '五',
        '6' => '六',
        '7' => '七',
        '8' => '八',
        '9' => '九',
        '10' => '十',
        '11' => '十一',
        '12' => '十二',
        '13' => '十三',
        '14' => '十四',
        '15' => '十五',
        '16' => '十六',
        '17' => '十七',
        '18' => '十八',
        '19' => '十九',
        '20' => '二十',
    ];

    // 购买节点
    public function buyNode($user_id, $buy_num)
    {
        $max_sn_id = self::getUserNode($user_id);

        for ($i = 1; $i <= $buy_num; $i++) {
            $max_sn_id++;

            if ($max_sn_id > self::getMaxBuyNodeNum()) {
                if (self::getLang() == 'en') {
                    throw new \Exception('Node is full');
                }
                throw new \Exception('节点已满');
            }
            $node = new self();
            $node->user_id = $user_id;
            $node->sn_id = $max_sn_id;
            $node->name = sprintf('节点%s', self::NODE_SN_ID[$max_sn_id]);
            $node->mint_num = 0;
            $node->total_mint_num = 0;
            $node->create_time = time();
            $node->save();
        }

        return true;
    }

    // 已购买节点数量
    public function getBuyNodeNum($user_id)
    {
        return (int) self::where('user_id', $user_id)->count();
    }

    // 剩余可购节点数量
    public function getCanBuyNodeNum($user_id)
    {
        return self::getMaxBuyNodeNum() - self::getBuyNodeNum($user_id);
    }

    // 获取用户节点最大序号
    public function getUserNode($user_id)
    {
        return (int) self::where('user_id', $user_id)->max('sn_id');
    }

    // 我的算力 = 我的节点数乘节点算力
    public function getUserNodePower($user_id)
    {
        return round(self::getBuyNodeNum($user_id) * self::getNodePower(), 2);
    }

    // 全网算力
    public function getAllNodePower()
    {
        return round(self::count() * self::getNodePower(), 2);
    }

    // 当日产币总数
    public function getUserDailyMintNum($user_id)
    {
        return self::where('user_id', $user_id)->sum('mint_num');
    }

    // 总产币数量
    public function getUserTotalMintNum($user_id)
    {
        return self::where('user_id', $user_id)->sum('total_mint_num');
    }

    // 每日产币
    public function mint()
    {
        $all_node_power = self::getAllNodePower();
        if($all_node_power <= 0){
            Log::info('全网算力为0');
            return;
        }

        self::query()->orderBy('id', 'asc')->chunk(100, function ($nodes) use ($all_node_power) { 
            foreach ($nodes as $node) { 
                $mint_num = round((self::getNodePower() / $all_node_power) * self::getAdminNodePower(), 6);
                $node->mint_num = $mint_num;
                $node->total_mint_num += $mint_num;
                $node->mint_time = time();
                $node->save();

                // 节点记录
                $log = new UserNodeLog();
                $log->user_id = $node->user_id;
                $log->node_id = $node->id;
                $log->mint_num = $mint_num;
                $log->create_time = time();
                $log->save();
                Log::info('节点ID:'.$node->id.' 产币:'.$mint_num);
            }
        });
    }

    /**
     * 节点名称
     *
     * @param  string  $value
     * @return string
     */
    public function getNameAttribute($value)
    {

        if (self::getLang() == 'en') {
            return sprintf('Node%s', $this->sn_id);
        }

        return $value;
    }

    public function getLang(){
        return request()->header('language');
    }
}
